<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$tabs=array(
	array('op'=>'list','title'=>'品牌代理等级'),
	array('op'=>'edit','title'=>'添加/编辑等级'),
);
set_html('tabs',$tabs);
$page_setting=$this->plugin_setting('page_setting');
$return['level_types']=array();
$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
foreach($return['levels'] as $rv){
	$return['level_types'][$rv['id']]=$rv['title'];
}
$return['level_types'][0]='默认等级';
if($_RQ['op']=='list'){	
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='level asc';
	$total='';
	$return['list']=$this->getall('level',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
		$item['conditions']=$page_setting['demolevel'];
	}
	//添加默认等级
	$default_level=$this->plugin_setting('default_level');
	$default_level=array(
		'id'=>'0',
		'title'=>$page_setting['demolevel']?:'默认等级',
		'level'=>0,
		'conditions'=>$page_setting['demolevel'],
		'memberrate'=>$default_level['memberrate'],
		'cirate'=>$default_level['cirate'],
		'zsrate'=>$default_level['zsrate'],
	);
	array_unshift($return['list'],$default_level);
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'<red>注意：多商户未独立分佣设置，将启用这里的等级设置！【属于店长】：前端可以查看所有商户的生态池</red>','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
			array('type'=>'string','title'=>'门店生态链分佣','name'=>'memberrate'),
			array('type'=>'string','title'=>'会员生态池分佣','name'=>'cirate'),
			array('type'=>'string','title'=>'培育员生态链分佣','name'=>'zsrate'),
			array('name'=>'isdian','title'=>'属于店长','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('op'=>'look_condition','title'=>'升级条件','type'=>'modal','show'=>array('level >'=>0)),
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0)),
			array('op'=>'set_default','title'=>'设置默认佣金','type'=>'href','show'=>array('level'=>0)),
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look_condition'){
	$item=$this->get('level',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']),array('conditions'));
	if(!$item){
		exi('指定等级不存在！','error');
	}
	
	$item['conditions']=json_decode($item['conditions'],true);
	$return['item']=$item;
	if(!$item['conditions']){
		exi('该等级尚未设置升级条件！','error');
	}
	$htmls=array(
		'type'=>'look',
		'look'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		$htmls['look'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['conditions']=$item['conditions']?json_decode($item['conditions'],true):array('order_price'=>'0');
	}else{
		$item=array(
			'title'=>'',
			'conditions'=>array('mygxfee'=>'0'),
		);
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'等级设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
		))),
	);
	//升级条件表单
	$level_updesc=$this->setting['is_level_up_one']?'满足其中一个就升级':'全满足才升级';
	$condition_edit=array(
		'title'=>'升级条件（'.$level_updesc.'）',
		'list'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		$condition_edit['list'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$htmls['edits'][]=$condition_edit;
	$commission_edit=array(
		'title'=>'设置',
		'list'=>array(
			//array('name'=>'tui_rate','title'=>'直推收益','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('type'=>'string','title'=>'门店生态链分佣','name'=>'memberrate','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('type'=>'string','title'=>'会员生态池分佣','name'=>'cirate','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('type'=>'string','title'=>'培育员生态链分佣','name'=>'zsrate','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
		),
	);
	$htmls['edits'][]=$commission_edit;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'level'=>intval($postdata['level']),
		'memberrate'=>$postdata['memberrate'],
		'cirate'=>$postdata['cirate'],
		'zsrate'=>$postdata['zsrate'],
		'conditions'=>$postdata['conditions']?ijson_encode($postdata['conditions']):'',
	);
	if($data['level']<=0 || $data['level']>100){
		exi('请填写正确的等级权重值，1-100','error');
	}
	if(!$data['title']){
		exi('请填写标题','error');
	}	
	$id=intval($postdata['id']);
	if($id){
		$this->update('level',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$this->insert('level',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set_default'){
	//设置默认佣金比例
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->plugin_setting('default_level',$post);
		exi('默认等级佣金设置成功！','','list');
	}
	$item=$this->plugin_setting('default_level');
	if(!$item){
		$item=array('ji_rate'=>'10%');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置默认佣金',
		'edit'=>array(
			array('type'=>'string','title'=>'门店生态链分佣','name'=>'memberrate','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('type'=>'string','title'=>'会员生态池分佣','name'=>'cirate','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('type'=>'string','title'=>'培育员生态链分佣','name'=>'zsrate','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
		),
	);
	$return['feetype']=$this->credit_type;
	$return['buy_creditfrom']=$this->buy_creditfrom;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('默认等级不能设置！','error');
	}
	$update=array();
	$post=get_postdata();
	$update['isdian']=intval($post['isdian']);
	pdo_update('jy_mlianfee_level',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_mlianfee_level',array('id'=>$id));	
	}
	if(!$id){
		exi('默认等级不能删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_mlianfee_level',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>